﻿namespace Yom.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    /// <summary>
    /// 分类数据操作
    /// 
    /// 创建: 邱柯皓 2009/01/20 13:36
    /// </summary>
    public class Category
    {
        #region 私有成员定义
        private readonly DBHelper db = new DBHelper();
        #endregion

        #region 构造函数
        public Category() { }
        #endregion

        #region 私有方法
        /// <summary>
        /// 通过DataReader获取分类实体对象
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:38
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>分类实体对象</returns>
        private Yom.Model.Category GetModel(SqlDataReader dr)
        {
            Yom.Model.Category model = new Yom.Model.Category();
            model.CategoryID = db.GetInt(dr["CategoryID"]);
            model.CategoryName = db.GetString(dr["CategoryName"]);
            model.SaveName = db.GetString(dr["SaveName"]);
            model.SeoDes = db.GetString(dr["SeoDes"]);
            model.SeoKey = db.GetString(dr["SeoKey"]);

            return model;
        }

        /// <summary>
        /// 通过DataReader获取分类列表
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:40
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>分类列表</returns>
        private List<Yom.Model.Category> GetList(SqlDataReader dr)
        {
            List<Yom.Model.Category> list = new List<Yom.Model.Category>();

            while (dr.Read())
                list.Add(GetModel(dr));

            return list;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 添加分类
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:42
        /// </summary>
        /// <param name="model">分类实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Add(Yom.Model.Category model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("INSERT INTO [Yom_Category] (");
            strSQL.Append("[CategoryName], [SaveName], [SeoKey], [SeoDes]");
            strSQL.Append(") VALUES (");
            strSQL.Append("@CategoryName, @SaveName, @SeoKey, @SeoDes)");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@CategoryName", DbType.String, model.CategoryName),
                db.CreateInSqlParameter("@SaveName", DbType.String, model.SaveName),
                db.CreateInSqlParameter("@SeoKey", DbType.String, model.SeoKey),
                db.CreateInSqlParameter("@SeoDes", DbType.String, model.SeoDes)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 修改分类
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:45
        /// </summary>
        /// <param name="model">分类实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Update(Yom.Model.Category model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE [Yom_Category] SET");
            strSQL.Append("[Categoryname]=@Categoryname, [SaveName]=@SaveName, [SeoKey]=@SeoKey, [SeoDes]=@SeoDes");
            strSQL.Append(" WHERE [CategoryID]=@CategoryID");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@CategoryName", DbType.String, model.CategoryName),
                db.CreateInSqlParameter("@SaveName", DbType.String, model.SaveName),
                db.CreateInSqlParameter("@SeoKey", DbType.String, model.SeoKey),
                db.CreateInSqlParameter("@SeoDes", DbType.String, model.SeoDes),
                db.CreateInSqlParameter("@CategoryID", DbType.Int32, model.CategoryID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 删除分类
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:48
        /// </summary>
        /// <param name="CategoryID">分类编号</param>
        /// <returns>返回成功标识</returns>
        public bool Delete(int CategoryID)
        {
            string strSQL = "DELETE FROM [Yom_Category] WHERE [CategoryID]=@CategoryID";
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@CategoryID", DbType.Int32, CategoryID) };

            return db.ExecuteNonQuery(CommandType.Text, strSQL, cmdParms) > 0;
        }

        /// <summary>
        /// 获取分类实体对象
        /// 
        /// 创建: 邱柯皓 2009/03/09 11:09
        /// </summary>
        /// <param name="CategoryID">分类编号</param>
        /// <returns>返回分类实体对象</returns>
        public Yom.Model.Category GetModel(int CategoryID)
        {
            string strSQL = "SELECT * FROM [Yom_Category] WHERE [CategoryID]=" + CategoryID.ToString();
            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                Yom.Model.Category model = new Yom.Model.Category();
                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 获取分类列表
        /// 
        /// 创建: 邱柯皓 2009/01/20 13:50
        /// </summary>
        /// <returns>返回分类列表</returns>
        public List<Yom.Model.Category> GetList()
        {
            string strSQL = "SELECT * FROM [Yom_Category]";

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL, null))
            {
                List<Yom.Model.Category> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 获取分类数量
        /// 
        /// 创建: 邱柯皓 2009/03/06 19:40
        /// </summary>
        /// <returns>返回分类数量</returns>
        public int GetCount()
        {
            string strSQL = "SELECT COUNT(*) FROM [Yom_Category]";
            object m = db.ExecuteScalar(CommandType.Text, strSQL, null);
            return db.GetInt(m);
        }
        #endregion
    }
}